package com.cositas.dulces.lp.promo2012;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class ControllerServlet extends HttpServlet {
	
	private static Map<String, Action> actions = new HashMap<String, Action>();
	
	static {
		actions.put("joinpromo", new JoinPromoAction());
		actions.put("rec", new RecommendAction());
		actions.put("ac", new AddCouponAction());
		actions.put("asc", new AddSharedCouponAction());
		actions.put("aq", new AddQuestionAction());
		actions.put("ec", new EnterCouponAction());
		actions.put("dp", new DownloadParticipantsAction());
		actions.put("dc", new DownloadCouponsAction());
		actions.put("dr", new DownloadRecommendationsAction());
		actions.put("dq", new DownloadQuestionsAction());
		actions.put("ansq", new AnswerQuestionAction());
		actions.put("ws", new WallShareAction());
		actions.put("vp", new VotePrizeAction());
		actions.put("gf", new GameFinishedAction());
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doPost(req, resp);
	}
	
	// TODO agregarle un password, ponerle un nombre bien raro
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		resp.setHeader("Cache-Control", "no-cache");
		resp.setDateHeader("Expires", 0);
		resp.setHeader("Pragma", "No-cache");
		String action = req.getParameter("action");
		try {
			actions.get(action).doAction(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				req.getRequestDispatcher("./ups.jsp").forward(req, resp);
			} catch (ServletException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
